//
//  CFHttpTools.m
//  WealthElves
//
//  Created by Wealthwiz on 2017/4/26.
//  Copyright © 2017年 Wealthwiz. All rights reserved.
//

#import "CFHttpTools.h"
#import <AFNetworking.h>

#import "LJTHUDHelper.h"

@implementation CFHttpTools

//GET请求
+ (void)getRequest:(NSString *)url params:(NSDictionary *)params success:(requestSuccessBlock)successHandler failure:(requestFailureBlock)failureHandler {
    
    if (![self checkNetWorkStatus]) {
        successHandler(nil);
        failureHandler(nil);
        return;
    }
    
    //拼接url
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",CFJL_SWIFET_URL,url];
    
    AFHTTPSessionManager *manager = [self getRequestManager];
    
    [manager GET:urlStr parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        id dict = [self responseConfiguration:responseObject];
        successHandler(dict);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failureHandler(error);
        [LJTHUDHelper showWaringHud:@"加载失败"];
        CFLog(@"%@",error.localizedDescription);
    }];
    
}

//POST请求
+ (void)postRequest:(NSString *)url params:(NSDictionary *)params success:(requestSuccessBlock)successHandler failure:(requestFailureBlock)failureHandler {
    
    
    if (![self checkNetWorkStatus]) {
        successHandler(nil);
        failureHandler(nil);
        return;
    }
    //拼接url
   // NSString *urlStr = [NSString stringWithFormat:@"%@%@", CFJL_SWIFET_URL, url];
    //AFHTTPSessionManager *manager = [self getRequestManager];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    [manager POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        id dict = [self responseConfiguration:responseObject];
        successHandler(dict);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        CFLog(@"-------请求失败-------%@",[error description].debugDescription);
        failureHandler(error);
    }];
    
}

//监控网络状态
+ (BOOL)checkNetWorkStatus {
    
    __block BOOL isNetWorkUse = YES;
    
    AFNetworkReachabilityManager *reachabilityManager = [AFNetworkReachabilityManager manager];
    [reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusUnknown) {
            isNetWorkUse = YES;
        }else if (status == AFNetworkReachabilityStatusReachableViaWiFi) {
            isNetWorkUse = YES;
        }else if (status == AFNetworkReachabilityStatusReachableViaWWAN) {
            isNetWorkUse = YES;
        }else {
            //网络操作异常
            isNetWorkUse = NO;
            [LJTHUDHelper showWaringHud:@"网络操作异常，请检查网络是否可用"];
        }
    }];
    [reachabilityManager startMonitoring];
    return isNetWorkUse;
}
+ (AFHTTPSessionManager *)getRequestManager {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //请求超时设定
    manager.requestSerializer.timeoutInterval = 5;
    manager.securityPolicy.allowInvalidCertificates = YES;
    
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    return manager;
}
+(id)responseConfiguration:(id)responseObject{
    
    NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
    
    return  dict;
}
@end
